package com.dc.ai.security.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.dc.ai.security.model.SysUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;


public class SecurityUtils {

    /**
     * 获取当前登录人信息
     * @return
     */
    public static SysUserDetails getUser() {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof SysUserDetails) {
                return (SysUserDetails) authentication.getPrincipal();
            }
        }
        return null;
    }

    /**
     * 获取用户ID
     *
     * @return Long
     */
    public static String getUserId() {
        SysUserDetails user = getUser();
        if (user == null) {
            // 返回默认值或抛出自定义异常
            return ""; // 或者其他默认值
        }
        return Convert.toStr(user.getUserId());
    }





}
